<template>
  <div class="login">
    <div class="left">
      <div class="login-title">
        <img class="t1" src="@/assets/img/login_logo.png" alt />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <el-form :model="form" class="form" :rules="rules" ref="form">
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
            v-model="form.phone"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
                v-model="form.code"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img :src="login_code" alt class="codeImg" @click="codeImg" />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" class="agree" :label="true">
            我已阅读并同意
            <el-link type="primary" class="link">用户协议</el-link>和
            <el-link type="primary" class="link">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn login_btn" @click="login_btn"
            >登录</el-button
          >
          <br />
          <el-button type="primary" class="btn" @click="toRegister"
            >注册</el-button
          >
        </el-form-item>
        <register ref="register"></register>
      </el-form>
    </div>
    <img class="right" src="@/assets/img/login_img.png" alt />
  </div>
</template>

<script>
import register from "./register";
import { login } from "@/api/login.js";
import { saveLocal, getLocal } from "@/utils/local.js";
export default {
  components: {
    register,
  },
  data() {
    return {
      login_code: process.env.VUE_APP_URL + "/captcha?type=login",
      login_token: "",
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: [],
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              let reg = /^1[3|4|5|7|8][0-9]{9}$/;
              0;

              if (reg.test(value)) {
                return callback();
              } else {
                return callback(new Error("请输入正确的手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 6, max: 18, message: "密码长度为6~18位", trigger: "change" },
        ],
        code: [{ required: true, message: "请输入手机号", trigger: "change" }],
        isPass: [
          { required: true, message: "请勾选协议", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value) {
                return callback();
              } else {
                return callback(new Error("请勾选协议"));
              }
            },
          },
        ],
      },
    };
  },
  methods: {
    codeImg() {
      this.login_code =
        process.env.VUE_APP_URL + "/captcha?type=login&hfasj" + Date.now();
    },
    // 登录按钮
    login_btn() {
      this.$refs.form.validate((result) => {
        if (result == true) {
          this.$message.success("验证成功");
          login(this.form).then((res) => {
            console.log(res);
            // window.localStorage.setItem("token", res.data.data.token);
            saveLocal("token", res.data.data.token);
            this.$router.push("/layout/chart");
            // console.log(res.data.data.token);
          });
        } else {
          this.$message.error("验证失败");
        }
      });
    },
    toRegister() {
      this.$refs.register.isShow = true;
    },
  },
  created() {
    // 访问基地址
    // console.log(getLocal("token"));
    if (getLocal("token")) {
      this.$router.push("/layout");
      return;
    }
    // alert(process.env.VUE_APP_URL);
  },
};
</script>

<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  justify-content: space-around;
  align-items: center;

  .left {
    width: 478px;
    height: 550px;
    background-color: #f5f5f5;
    padding: 42px;
  }
}
.login-title {
  display: flex;
  align-items: center;
  .t1 {
    width: 22px;
    height: 17px;
    margin-right: 14px;
  }
  .t2,
  .t4 {
    font-size: 24px;
    font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
    font-weight: 400;
    text-align: left;
    color: #0c0c0c;
  }
  .t3 {
    width: 1px;
    height: 28px;
    background: #c7c7c7;
    margin: 0 14px;
  }
}
.form {
  margin-top: 30px;
}
.codeImg {
  width: 100%;
  height: 40px;
}
.btn {
  width: 100%;
  display: block;
}
.btn:nth-child(2) {
  margin-top: 20px;
}
.link {
  vertical-align: baseline;
}
</style>
